package com.jsb.laundry.common.utils.job;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * spring线程池
 */
@Configuration
@EnableAsync
public class AsyncScheduledTaskConfig {
    // 获取CPU核数
    private static final int availableProcessors = Runtime.getRuntime().availableProcessors();
    // 核心线程池数
//    @Value("${spring.task.execution.pool.core-size}")
    private int corePoolSize = availableProcessors;
    // 最大线程池数
//    @Value("${spring.task.execution.pool.max-size}")
    private int maxPoolSize = availableProcessors * 2;
    // 任务队列的容量
//    @Value("${spring.task.execution.pool.queue-capacity}")
    private int queueCapacity = 2000;
    // 线程池的前缀名称
//    @Value("${spring.task.execution.thread-name-prefix}")
    private String namePrefix = "my-";
    // 非核心线程的存活时间
//    @Value("${spring.task.execution.pool.keep-alive}")
    private int keepAliveSeconds = 60;

    @Bean(name = "myAsync")
    public Executor myAsync() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //最大线程数
        executor.setMaxPoolSize(maxPoolSize);
        //核心线程数
        executor.setCorePoolSize(corePoolSize);
        //任务队列的大小
        executor.setQueueCapacity(queueCapacity);
        //线程前缀名
        executor.setThreadNamePrefix(namePrefix);
        //线程存活时间
        executor.setKeepAliveSeconds(keepAliveSeconds);

        /**
         * 拒绝处理策略
         * CallerRunsPolicy()：交由调用方线程运行，比如 main 线程。
         * AbortPolicy()：直接抛出异常。
         * DiscardPolicy()：直接丢弃。
         * DiscardOldestPolicy()：丢弃队列中最老的任务。
         */
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        //线程初始化
        executor.initialize();
        return executor;
    }
}
